package com.cali.libcore.dialog.alertdialog.helper

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.cali.libcore.dialog.alertdialog.AlertDialog

/**
 * @Author HaoXin
 * @Date 2020/11/14
 */
class CommonDialogHelper(context: Context) {

    private var alertDialog: AlertDialog? = null

    private var commonBuilder: AlertDialog.Builder = AlertDialog.Builder(context)
            .addDefaultAnimation()
            .setDim(0.3f)

    fun show(view: View, bottom: Boolean = true, isMath: Boolean = true) {
        view.parent?.apply {
            this as ViewGroup
            this.removeAllViews()
        }
        val width = if(isMath) ViewGroup.LayoutParams.MATCH_PARENT else
            ViewGroup.LayoutParams.WRAP_CONTENT
        if (bottom) {
            commonBuilder.fromBottom(true)
        }
        alertDialog = commonBuilder
            .setCancelable(false)
            .setContentView(view)
            .setWidthAndHeight(width,
                ViewGroup.LayoutParams.WRAP_CONTENT)
            .create()
        alertDialog?.show()
    }

    fun dismiss() {
        alertDialog?.dismiss()
    }

}